package action;

import character.MyCharacter;
import character.MyCharacterControl;
import com.jme3.math.Vector3f;
import game.CombatController;
import game.CombatUtil;

public class ActionAttack extends Action {
    protected MyCharacter target;

    @Deprecated
    protected ActionAttack() {}
    
    public ActionAttack(MyCharacter attacker, MyCharacter target) {
        super(attacker, attacker.getInventory().getEquippedWeapon().getBaseSpeed());
        this.target = target;
    }

    public ActionAttack(boolean offTurnAction, MyCharacter attacker, MyCharacter target, int ticks) {
	super(offTurnAction, attacker, ticks);
	this.target = target;
    }

    @Override
    protected void start() {
        updateViewDir();
        System.out.println(character+" swings its arm back...");
    }

    @Override
    protected boolean tick() {
        updateViewDir();
        if(!CombatUtil.checkAttackRange(character, target)) {
            System.out.println("Target moved out of range.");
            abortAction();
        }
        return true;
    }

    @Override
    protected void complete() {
        updateViewDir();
        if(CombatUtil.checkAttackRange(character, target)) {
            CombatController.attack(character, target);
        } else {
            System.out.println("Target is out of range.");
            abortAction();
        }
    }
    
    @Override
    protected void abort() {}

    @Override
    public void pause() {}

    @Override
    public void unpause() {}
    
    protected void updateViewDir() {
        Vector3f viewDir = target.getControl(MyCharacterControl.class).getPhysicsLocation().subtract(charControl.getPhysicsLocation());
        viewDir.multLocal(1f, 0f, 1f);
        charControl.setViewDirection(viewDir);
    }
}
